// 补零
let zeroFill = function (n) {
    n = String(n)
    return n[1] ? n : 0 + n
}

/**
 * 格式化时间
 * @param {*} time 时间戳
 * @param {*} format 日期时间格式
 * @returns 格式化后的时间
 */
export function parseTime(time, format = 'yyyy-MM-dd HH:mm:ss') {
    if (!time) return null
    let allFormat = ['yyyy', 'MM', 'M', 'mm', 'm', 'dd', 'd', 'HH', 'H', 'hh', 'h', 'mm', 'm', 'ss', 's']
    let date = new Date(time)
    let dateObj = {
        yyyy: date.getFullYear(),
        MM: zeroFill(date.getMonth() + 1),
        dd: zeroFill(date.getDate()),
        HH: zeroFill(date.getHours()),
        mm: zeroFill(date.getMinutes()),
        ss: zeroFill(date.getSeconds())
    }
    for (let i = 0; i < allFormat.length; i++) {
        format = format.replace(allFormat[i], dateObj[allFormat[i]])
    }
    return format
}

/* 通用下载文件
* @param {*} obj 数据源
* @param {*} name 文件名称
* @param {*} suffix 文件后缀
*/
export function downloadFile(obj, name, suffix) {
    const url = window.URL.createObjectURL(new Blob([obj]))
    const link = document.createElement('a')
    link.style.display = 'none'
    link.href = url
    let myDate = new Date()
    let time = myDate.toLocaleDateString().split('/').join('-')
    const fileName = time + '-' + name + '.' + suffix
    link.setAttribute('download', fileName)
    document.body.appendChild(link)
    link.click()
    document.body.removeChild(link)
}

/**
 * xls表格导出
 * @param {String} header 表头字段
 * @param {Array} props 字段
 * @param {Array} sourceData 数据（数组对象）
 * @param {String} fileName 文件名
 */
export function xlsExport(obj) {
    let {
        header = [],
        props = [],
        sourceData = [],
        fileName = 'fileName'
    } = obj
    // 生成表头
    let thead = '<tr>'
    for (let i = 0; i < header.length; i++) {
        thead += `<td style="font-size: 16px;background-color: #eef1f6;text-align: center;">${header[i]}</td>`
    }
    thead += '</tr>'
    // 生成表身
    let tbody = ''
    for (let i = 0; i < sourceData.length; i++) {
        tbody += '<tr>'
        for (let k of props) {
            tbody += `<td style="text-align: center;width: 150px;height: 50px;">${(sourceData[i][k] !== null ? sourceData[i][k].toString() : '') + '\t'}</td>`
        }
        tbody += '</tr>'
    }
    let excel = '<table>' + thead + tbody + '</table>'

    //下载的表格模板数据
    var excelFile = '<html xmlns:o="urn:schemas-microsoft-com:office:office" ' +
        'xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40">'
    excelFile += '<meta http-equiv="content-type" content="application/vnd.ms-excel; charset=UTF-8">'
    excelFile += '<meta http-equiv="content-type" content="application/vnd.ms-excel"'
    excelFile += ' charset="UTF-8">'
    excelFile += '<head>'
    excelFile += '<!--[if gte mso 9]>'
    excelFile += '<xml>'
    excelFile += '<x:ExcelWorkbook>'
    excelFile += '<x:ExcelWorksheets>'
    excelFile += '<x:ExcelWorksheet>'
    excelFile += '<x:Name>'
    excelFile += 'sheet'
    excelFile += '</x:Name>'
    excelFile += '<x:WorksheetOptions>'
    excelFile += '<x:DisplayGridlines/>'
    excelFile += '</x:WorksheetOptions>'
    excelFile += '</x:ExcelWorksheet>'
    excelFile += '</x:ExcelWorksheets>'
    excelFile += '</x:ExcelWorkbook>'
    excelFile += '</xml>'
    excelFile += '<![endif]-->'
    excelFile += '</head>'
    excelFile += '<body>'
    excelFile += excel
    excelFile += '</body>'
    excelFile += '</html>'

    //下载模板
    let uri = 'data:application/vnd.ms-excelcharset=utf-8,' + encodeURIComponent(excelFile)
    let link = document.createElement('a')
    link.href = uri
    link.style = 'visibility:hidden'
    let myDate = new Date()
    let time = myDate.toLocaleDateString().split('/').join('-')
    link.download = fileName + '-' + time + '.xls'
    document.body.appendChild(link)
    link.click()
    document.body.removeChild(link)
}

/**
 * 简单word导出
 * @param {String} content word内容
 * @param {String} fileName 文件名
 */
export function wordExport(content = '', fileName = 'fileName') {
    //下载的模板数据
    var excelFile = '<html xmlns:o="urn:schemas-microsoft-com:office:office" ' +
        'xmlns:x="urn:schemas-microsoft-com:office:word" xmlns="http://www.w3.org/TR/REC-html40">'
    excelFile += '<meta http-equiv="content-type" content="application/vnd.ms-word; charset=UTF-8">'
    excelFile += '<meta http-equiv="content-type" content="application/vnd.ms-word"'
    excelFile += ' charset="UTF-8">'
    excelFile += '<head>'
    excelFile += '<!--[if gte mso 9]>'
    excelFile += '<xml>'
    excelFile += '<x:WordWorkbook>'
    excelFile += '<x:WordWorksheets>'
    excelFile += '<x:WordWorksheet>'
    excelFile += '<x:Name>'
    excelFile += 'sheet'
    excelFile += '</x:Name>'
    excelFile += '<x:WorksheetOptions>'
    excelFile += '<x:DisplayGridlines/>'
    excelFile += '</x:WorksheetOptions>'
    excelFile += '</x:WordWorksheet>'
    excelFile += '</x:WordWorksheets>'
    excelFile += '</x:WordWorkbook>'
    excelFile += '</xml>'
    excelFile += '<![endif]-->'
    excelFile += '</head>'
    excelFile += '<body>'
    excelFile += content
    excelFile += '</body>'
    excelFile += '</html>'

    //下载模板
    let uri = 'data:application/vnd.ms-word;charset=utf-8,' + encodeURIComponent(excelFile)
    let link = document.createElement('a')
    link.href = uri
    link.style = 'visibility:hidden'
    let myDate = new Date()
    let time = myDate.toLocaleDateString().split('/').join('-')
    link.download = fileName + time + '.doc'
    document.body.appendChild(link)
    link.click()
    document.body.removeChild(link)
}
export function wordExport2(content = '', fileName = 'fileName') {
    let wordFile = `
        <html xmlns:v=3D"urn:schemas-microsoft-com:vml"
        xmlns:o=3D"urn:schemas-microsoft-com:office:office"
        xmlns:w=3D"urn:schemas-microsoft-com:office:word"
        xmlns:m=3D"http://schemas.microsoft.com/office/2004/12/omml"
        xmlns=3D"http://www.w3.org/TR/REC-html40">
        <head>
        <meta http-equiv="3DContent-Type" content="3Dtext/html;" charset="3Dutf-8">
        <meta name="3DProgId" content="3DWord.Document">
        <meta name="3DGenerator" content="3DMicrosoft Word 15">
        <meta name="3DOriginator" content="3DMicrosoft Word 15">
        </head>
        <body lang="3DZH-CN" style="3Dtab-interval:21.0pt">${content}</body>
        </html>
        `
    //下载模板
    let uri = 'data:application/vnd.ms-word;charset=utf-8,' + encodeURIComponent(wordFile)
    let link = document.createElement('a')
    link.href = uri
    link.style = 'visibility:hidden'
    let myDate = new Date()
    let time = myDate.toLocaleDateString().split('/').join('-')
    link.download = fileName + time + '.doc'
    document.body.appendChild(link)
    link.click()
    document.body.removeChild(link)
}
